package com.project.nimsen.web.sys;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.nimsen.dto.SiteDto;
import com.project.nimsen.entity.Modelinfo;
import com.project.nimsen.entity.Site;
import com.project.nimsen.service.IModelinfoService;
import com.project.nimsen.service.ISiteService;
import com.project.nimsen.util.NimsenUtil;
import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.Page;
import com.project.jdbc.web.BaseMVCAction;
/**
 * 站点Control
 * */
@Controller
@RequestMapping(value ="/sys/site")
public class SiteControl extends BaseMVCAction {
	@Resource
	ISiteService siteService;
	@Resource
	IModelinfoService modelService;
	@RequestMapping(value ="/list")
	public String list(){
		return "/WEB-INF/sys/site/list";
	}
	/**
	 * 查询方法
	 * */
	@RequestMapping(value="search")
	@ResponseBody
	public Map<String, Object> search(){
		//分页信息
		Page page = new Page(getIntegerParameter("page"));
		page.setPageSize(getIntegerParameter("rows"));
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", 0);
		SiteDto dto = new SiteDto();
		dto.setPage(page);
		try {
			List<Map<String, Object>> rows = siteService.find(dto);
			if(CollectionUtils.isEmpty(rows)){
				map.put("rows", "");
				return map;
			}
			//转换时间
			for(Map<String, Object> row : rows){
				Date createTime = (Date)row.get("createTime");
				row.put("createTime", DateUtils.convertDate2String(createTime));
			}
			map.put("rows", rows);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 向前台返回一个实体
	 * */
	@RequestMapping(value = "/bean")
	@ResponseBody
	public Map<String, Object> bean(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		SiteDto dto = new SiteDto();
		dto.setId(getParameter("id"));
		try {
			Map<String, Object> entity = siteService.findByDto(dto);
			if(entity==null){
				map.put("success", false);
				map.put("message", " 找不到要配置的站点！");
				return map;
			}
			//PC模板信息
			String modelid = NimsenUtil.obj2String(entity.get("modelid"));
			if(StringUtils.isNotBlank(modelid)){
				Modelinfo model = modelService.findEntityById(modelid);
				map.put("modelname", model.getModelname());
			}else{
				entity.put("modelid", "");
				map.put("modelname", "");
			}
			//phone模板信息
			String phonemodelid = NimsenUtil.obj2String(entity.get("phonemodelid"));
			if(StringUtils.isNotBlank(phonemodelid)){
				Modelinfo model = modelService.findEntityById(phonemodelid);
				map.put("phonemodelname", model.getModelname());
			}else{
				entity.put("phonemodelid", "");
				map.put("phonemodelname", "");
			}
			//pad模板信息
			String padmodelid = NimsenUtil.obj2String(entity.get("padmodelid"));
			if(StringUtils.isNotBlank(padmodelid)){
				Modelinfo model = modelService.findEntityById(padmodelid);
				map.put("padmodelname", model.getModelname());
			}else{
				entity.put("padmodelid", "");
				map.put("padmodelname", "");
			}
			map.put("success", true);
			map.put("bean", entity);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			map.put("success", false);
		}
		return map;
	}
	/**
	 * 修改一个站点
	 * */
	@RequestMapping(value ="/add")
	public void add(HttpServletRequest request,HttpServletResponse response){
		
		String id = getParameter("id");
		String sitetag = getParameter("sitetag");
		try {
			if(StringUtils.isBlank(id)){
				response.getWriter().println(false);
				return;
			}
			Site site = siteService.findEntityById(id);
			site.setSitetag(sitetag);
			String domname = getParameter("domainname");
			site.setDomainname(domname);
			//模板信息设置
			site.setIndexmodel(getParameter("indexmodel"));
			site.setPhoneindexmodel(getParameter("phoneindexmodel"));
			site.setPadindexmodel(getParameter("padindexmodel"));
			site.setModelid(getParameter("modelid"));
			site.setPhonemodelid(getParameter("phonemodelid"));
			site.setPadmodelid(getParameter("padmodelid"));
			siteService.update(site);
			response.getWriter().println(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			try {
				response.getWriter().println(false);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	/**
	 * 修改时的验证信息
	 * */
	@RequestMapping(value ="/validate")
	@ResponseBody
	public Map<String, Object> validate(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			SiteDto dto = new SiteDto();
			dto.setValifield("domainname");
			dto.setValivalue(getParameter("domainname"));
			dto.setId(getParameter("id"));
			Map<String, Object> colnameMap = siteService.findVilidate(dto);
			boolean validate = colnameMap==null;
			
			if(!validate)
				map.put("message", "已经有"+dto.getValivalue()+"的域名绑定过!");
			else
				map.put("message", "");
			map.put("success", validate);
		} catch(Exception e){
			e.printStackTrace();
			logger.error(e);
		}
		return map;
	}
}
